﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Xceed.Chart.GraphicsCore;
using Xceed.Chart.Standard;
using Xceed.Chart;
using Xceed.Chart.Core;

namespace Gadgets.Trading
{
    public partial class eTotalMarket : WeifenLuo.WinFormsUI.Docking.DockContent  
    {
        public eTotalMarket()
        {
            InitializeComponent();
        }

        private long hose_timestamp = 0;
        private long hnx_timestamp = 0;
        
        private void eTotalMarket_Load(object sender, EventArgs e)
        {
            try
            {
                hose_timestamp = 0;
                ABSCommonLibrary.InitChartControl(ref chartControlHoseIndex);
                // add the line
                LineSeries m_Line = (LineSeries)chartControlHoseIndex.Charts[0].Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Indigo;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;               
                m_Line.Name = "VN-Index";

                // instruct the line series to use x values
                m_Line.UseXValues = true;

                hnx_timestamp = 0;
                ABSCommonLibrary.InitChartControl(ref chartControlHnxIndex);
                // add the line
                m_Line = (LineSeries)chartControlHnxIndex.Charts[0].Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Indigo;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;
                m_Line.Name = "HNX-Index";

                // instruct the line series to use x values
                m_Line.UseXValues = true;
                
                ABSCommonLibrary.InitChartControl(ref chartControlHoseTrend);

                // add the line
                m_Line = (LineSeries)chartControlHoseTrend.Charts[0].Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Blue;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;                
                m_Line.Name = "Advances";

                // instruct the line series to use x values
                m_Line.UseXValues = true;

                m_Line = (LineSeries)chartControlHoseTrend.Charts[0].Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Red;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;               
                m_Line.Name = "Declines";

                // instruct the line series to use x values
                m_Line.UseXValues = true;

                m_Line = (LineSeries)chartControlHoseTrend.Charts[0].Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Yellow;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;
                m_Line.Name = "Nochange";

                // instruct the line series to use x values
                m_Line.UseXValues = true;

                ABSCommonLibrary.InitChartControl(ref chartControlHnxTrend);

                // add the line
                m_Line = (LineSeries)chartControlHnxTrend.Charts[0].Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Blue;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;
                m_Line.Name = "Advances";

                // instruct the line series to use x values
                m_Line.UseXValues = true;

                m_Line = (LineSeries)chartControlHnxTrend.Charts[0].Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Red;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;
                m_Line.Name = "Declines";

                // instruct the line series to use x values
                m_Line.UseXValues = true;

                m_Line = (LineSeries)chartControlHnxTrend.Charts[0].Series.Add(SeriesType.Line);
                m_Line.LineStyle = LineSeriesStyle.Line;
                m_Line.DataLabels.Mode = DataLabelsMode.None;
                m_Line.Legend.Mode = SeriesLegendMode.Series;
                m_Line.LineBorder.Color = Color.Yellow;
                m_Line.LineBorder.Width = 2;
                m_Line.InflateMargins = true;
                m_Line.Markers.Visible = false;
                m_Line.Name = "Nochange";

                // instruct the line series to use x values
                m_Line.UseXValues = true; 

                DataSet last_market = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC010002,
                    new object[1] { "HOSE" });
                if (last_market.Tables[0].Rows.Count >= 0)
                {
                    DataRow row = last_market.Tables[0].Rows[0];
                    Chart m_Chart = chartControlHoseIndex.Charts[0];

                    m_Chart.Axis(StandardAxis.PrimaryX).ScaleMode = AxisScaleMode.Numeric;
                    m_Chart.Axis(StandardAxis.PrimaryY).ScaleMode = AxisScaleMode.Numeric;
                    
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Min = Convert.ToDouble(row["OPEN_TIME"]);
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Max = Convert.ToDouble(row["CLOSE_TIME"]);
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto;                    
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Min = Convert.ToDouble(row["FLOOR_INDEX"]);
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Max = Convert.ToDouble(row["CEILING_INDEX"]);
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto;                   
                    
                    m_Chart = chartControlHoseTrend.Charts[0];
                    m_Chart.Axis(StandardAxis.PrimaryX).ScaleMode = AxisScaleMode.Numeric;
                    m_Chart.Axis(StandardAxis.PrimaryY).ScaleMode = AxisScaleMode.Numeric;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Min = Convert.ToDouble(row["OPEN_TIME"]);
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Max = Convert.ToDouble(row["CLOSE_TIME"]);
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto;                   
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Min = 0;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Max = Convert.ToDouble(row["TOTAL_STOCK"]);
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto;
                }

                last_market = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC010002,
                    new object[1] { "HNX" });
                if (last_market.Tables[0].Rows.Count >= 0)
                {
                    DataRow row = last_market.Tables[0].Rows[0];
                    Chart m_Chart = chartControlHnxIndex.Charts[0];

                    m_Chart.Axis(StandardAxis.PrimaryX).ScaleMode = AxisScaleMode.Numeric;
                    m_Chart.Axis(StandardAxis.PrimaryY).ScaleMode = AxisScaleMode.Numeric;

                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Min = Convert.ToDouble(row["OPEN_TIME"]);
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Max = Convert.ToDouble(row["CLOSE_TIME"]);
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Min = Convert.ToDouble(row["FLOOR_INDEX"]);
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Max = Convert.ToDouble(row["CEILING_INDEX"]);
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto;

                    m_Chart = chartControlHnxTrend.Charts[0];
                    m_Chart.Axis(StandardAxis.PrimaryX).ScaleMode = AxisScaleMode.Numeric;
                    m_Chart.Axis(StandardAxis.PrimaryY).ScaleMode = AxisScaleMode.Numeric;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Min = Convert.ToDouble(row["OPEN_TIME"]);
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.Max = Convert.ToDouble(row["CLOSE_TIME"]);
                    m_Chart.Axis(StandardAxis.PrimaryX).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMin = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Min = 0;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.AutoMax = false;
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.Max = Convert.ToDouble(row["TOTAL_STOCK"]);
                    m_Chart.Axis(StandardAxis.PrimaryY).NumericScale.MajorTickMode = MajorTickModeNumeric.Auto;
                }

                timerUpdate.Enabled = true;
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
        private void UpdateChart()
        {
            DataSet total_market = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC010010,
                    new object[2] { "HOSE", hose_timestamp });
            LineSeries m_Line = (LineSeries)chartControlHoseIndex.Charts[0].Series[0];
            // add xy values           
            for (int i = 0; i < total_market.Tables[0].Rows.Count; i++)
            {                
                DataRow row = total_market.Tables[0].Rows[i];                
                m_Line.AddXY(Convert.ToDouble(row["BOARD_INDEX"]),
                    Convert.ToDouble(row["TIMESTAMP"]));
                if (hose_timestamp < Convert.ToInt32(row["TIMESTAMP"]))
                    hose_timestamp = Convert.ToInt32(row["TIMESTAMP"]);
            }

            m_Line = (LineSeries)chartControlHoseTrend.Charts[0].Series[0];
            // add xy values
          
            for (int i = 0; i < total_market.Tables[0].Rows.Count; i++)
            {
                DataRow row = total_market.Tables[0].Rows[i];
                m_Line.AddXY(Convert.ToDouble(row["ADVANCES"]),
                    Convert.ToDouble(row["TIMESTAMP"]));
            }

            m_Line = (LineSeries)chartControlHoseTrend.Charts[0].Series[1];
            // add xy values
           
            for (int i = 0; i < total_market.Tables[0].Rows.Count; i++)
            {
                DataRow row = total_market.Tables[0].Rows[i];
                m_Line.AddXY(Convert.ToDouble(row["DECLINES"]),
                    Convert.ToDouble(row["TIMESTAMP"]));
            }

            m_Line = (LineSeries)chartControlHoseTrend.Charts[0].Series[2];
            // add xy values
           
            for (int i = 0; i < total_market.Tables[0].Rows.Count; i++)
            {
                DataRow row = total_market.Tables[0].Rows[i];
                m_Line.AddXY(Convert.ToDouble(row["NOCHANGE"]),
                    Convert.ToDouble(row["TIMESTAMP"]));
            }

            chartControlHoseTrend.Refresh();
            chartControlHoseIndex.Refresh();

            //Cap nhat HNX
            total_market = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC010010,
                    new object[2] { "HNX", hnx_timestamp });
            m_Line = (LineSeries)chartControlHnxIndex.Charts[0].Series[0];
            // add xy values           
            for (int i = 0; i < total_market.Tables[0].Rows.Count; i++)
            {
                DataRow row = total_market.Tables[0].Rows[i];
                m_Line.AddXY(Convert.ToDouble(row["BOARD_INDEX"]),
                    Convert.ToDouble(row["TIMESTAMP"]));
                if (hnx_timestamp < Convert.ToInt32(row["TIMESTAMP"]))
                    hnx_timestamp = Convert.ToInt32(row["TIMESTAMP"]);
            }

            m_Line = (LineSeries)chartControlHnxTrend.Charts[0].Series[0];
            // add xy values

            for (int i = 0; i < total_market.Tables[0].Rows.Count; i++)
            {
                DataRow row = total_market.Tables[0].Rows[i];
                m_Line.AddXY(Convert.ToDouble(row["ADVANCES"]),
                    Convert.ToDouble(row["TIMESTAMP"]));
            }

            m_Line = (LineSeries)chartControlHnxTrend.Charts[0].Series[1];
            // add xy values

            for (int i = 0; i < total_market.Tables[0].Rows.Count; i++)
            {
                DataRow row = total_market.Tables[0].Rows[i];
                m_Line.AddXY(Convert.ToDouble(row["DECLINES"]),
                    Convert.ToDouble(row["TIMESTAMP"]));
            }

            m_Line = (LineSeries)chartControlHnxTrend.Charts[0].Series[2];
            // add xy values

            for (int i = 0; i < total_market.Tables[0].Rows.Count; i++)
            {
                DataRow row = total_market.Tables[0].Rows[i];
                m_Line.AddXY(Convert.ToDouble(row["NOCHANGE"]),
                    Convert.ToDouble(row["TIMESTAMP"]));
            }

            chartControlHnxTrend.Refresh();
            chartControlHnxIndex.Refresh();
        }
        
        private void eTotalMarket_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F9)
                {                    
                    chartControlHoseTrend.Refresh();
                    chartControlHoseIndex.Refresh();
                    chartControlHnxTrend.Refresh();
                    chartControlHnxIndex.Refresh();
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void timerUpdate_Tick(object sender, EventArgs e)
        {
            try
            {
                timerUpdate.Enabled = false;
                UpdateChart();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
            finally
            {
                timerUpdate.Enabled = true;
            }
        }

        private void chartControlHoseIndex_DoubleClick(object sender, EventArgs e)
        {
            
            chartControlHoseIndex.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax
                = !chartControlHoseIndex.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax;
            chartControlHoseIndex.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin
                = !chartControlHoseIndex.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin;
            chartControlHoseIndex.Refresh();

        }

        private void chartControlHoseTrend_DoubleClick(object sender, EventArgs e)
        {
            chartControlHoseTrend.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax
                = !chartControlHoseTrend.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax;
            chartControlHoseTrend.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin
                = !chartControlHoseTrend.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin;
            chartControlHoseTrend.Refresh();
        }

        private void chartControlHnxIndex_DoubleClick(object sender, EventArgs e)
        {
            chartControlHnxIndex.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax
                = !chartControlHnxIndex.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax;
            chartControlHnxIndex.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin
                = !chartControlHnxIndex.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin;
            chartControlHnxIndex.Refresh();
        }

        private void chartControlHnxTrend_DoubleClick(object sender, EventArgs e)
        {
            chartControlHnxTrend.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax
                = !chartControlHnxTrend.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMax;
            chartControlHnxTrend.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin
                = !chartControlHnxTrend.Charts[0].Axis(StandardAxis.PrimaryY).NumericScale.AutoMin;
        }        
    }
}